// PROGRAMA : TABPM001 OPCAO: 'ATE'
// BIBLIOTECA : SISP
// SISTEMA : INFORMACOES DE SEGURANCA PUBLICA - SISP
// CLIENTE : GEJUSPC - GERENCIA DA JUSTICA, SEGURANCA PUBLICA E
// CIDADANIA / INSTITUTO DE IDENTIFICACAO
// PROGRAMADOR : * DATA: 27/06/2002
// OBJETIVO : ATUALIZAR TABELA DE ENDERECO DE TERMINAL
// ATUALIZACOES DATA
// PROGRAMA : TABTM001 27/06/2002
// BIBLIOTECA : SISP
// PROGRAMADOR :
// OBJETIVO :
package br.com.vti.sisp_sicrim.antigo.controller.programa;

import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Sistha01;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Sistha02;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Tabtmp01;
import br.com.vti.sisp_sicrim.antigo.model.SicTabelas;
import br.com.vti.sisp_sicrim.antigo.model.Sislac01;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.Sisgsisp;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Tabtm001 OBJETIVO : ATUALIZAR TABELA DE ENDERECO DE TERMINAL *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/tabtm001")
public class Tabtm001 {
	// Views utilizadas
	SicTabelas vwDvetab = new SicTabelas();
	// Telas/relatorios utilizados
	Tabtmp01 tabtmp01;
	Sistha01 sistha01;
	Sistha02 sistha02;
	// Includes utilizados
	Sislac01 sislac01 = new Sislac01();
	// Areas globais / session
	Sisgsisp sisgsisp;
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();

	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	Tabtm001 tabtm001Anterior;
	String faseproc = "0";

	// EXCLUSAO
	private String descel1 = "";
	// DESCRICAO DO TERMINAL
	private String observ = " ";
	// DESCRICAO DO TERMINAL
	private int datapo = 0;
	private String grupel = "";
	private String s1 = "";
	private int data = 0;
	private int date = 0;

	// 3 B2 (A1)
	private Tabtmp01 preMapas() {
		tabtmp01 = new Tabtmp01(true, sessao);
		sistha01 = new Sistha01(true, sessao);
		sistha02 = new Sistha02(true, sessao);
		tabtmp01.setDataCorrente(UtilBean.inverteDataCorrente());
		date = UtilBean.obtemDataCorrente();
		data = UtilBean.inverteData(date);
		// Par�grafo: r0()
		return tabtmp01;
	}

	public Tabtm001(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public void executar(Sistha01 sistha01, Sistha02 sistha02, Tabtmp01 tabtmp01) {
		try {
			main(sistha01, sistha02, tabtmp01);
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroSiscerro(e, sessao, this.getClass().getSimpleName());
		}
	}

	private void main(Sistha01 psistha01, Sistha02 psistha02, Tabtmp01 ptabtmp01) {
		if (psistha01 == null && psistha02 == null && ptabtmp01 == null) {
			primeiraVez = true;
			// tabtmp01 = new Tabtmp01 (primeiraVez);
			tabtmp01 = preMapas();
			tabtmp01.setNavegar("../tabtm001/executar");
			result.include("tabtmp01", tabtmp01);
			sessao.setAtributo("TABTM001", this);
			tabtmp01.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		} else {
			restaurarEstados(psistha01, psistha02, ptabtmp01);
		}

		observ = " ";
		grupel = "";
		datapo = 0;
		s1 = "";
		// mens = "";
		// ctl1 = "";
		// ctl2 = "";
		// op1 = "";
		// op2 = "";
		// op3 = "";
		sessao.getSisgsisp().setVar(10);

		s1 = UtilBean.moveSubstring(s1, 0, 3, "N", 889);
		tabtmp01.setSn("N");

		switch (faseproc) {
		case "0": {
			tabtmp01.receberTela(false, result);
			if (!tabtmp01.getOperacao().equals("ATE")) {
				saida();
			}
			if (vwDvetab.pesquisaTabcha(889, "")) {
				tabtmp01.setMensagemSaida("REGISTRO EXISTENTE - TECLE PF5 PARA ATUALIZAR");
				descel1 = vwDvetab.getDescricaoElem();
				datapo = UtilBean.obtemDataCorrente();

				faseproc = "1";
				sessao.setAtributo("TABTM001", this);
				result.include("tabtmp01", tabtmp01);
				tabtmp01.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				return;
			} else {
				tabtmp01.setMensagemSaida("REGISTRO INEXISTENTE - TECLE 'ENTER' PRA INCLUIR");
				tabtmp01.setSn("S");
				faseproc = "4";
				sessao.setAtributo("TABTM001", this);
				result.include("tabtmp01", tabtmp01);
				tabtmp01.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				return;
			}
		}
		case "1": {
			tabtmp01.receberTela(false, result);
			if (!tabtmp01.getOperacao().equals("ATE")) {
				saida();
			}
			if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
				tabtmp01.setMensagemSaida("ALTERACAO NAO FOI EXECUTADA");
				sessao.setAtributo("TABTM001", this);
				result.include("tabtmp01", tabtmp01);
				tabtmp01.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (UtilBean.funcao.equals(UtilBean.ctt_proxima)) {

				faseproc = "2";
				sessao.setAtributo("TABTM001", this);
				result.include("tabtmp01", tabtmp01);
				sessao.setAtributo("TABTM001", this);
				tabtmp01.enviarTelaConfirmacao("op3", result, getClass().getSimpleName().toUpperCase());
				return;
			}
			faseproc = "3";
			sessao.setAtributo("TABTM001", this);
			result.include("sistha01", sistha01);
			sistha01.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		case "2": {
			if (vwDvetab.pesquisaTabcha(889, "")) {
				descel1 = vwDvetab.getDescricaoElem();
				datapo = UtilBean.obtemDataCorrente();
				tabtmp01.receberTela(false, result);
				if (!(tabtmp01.getOp3().equals("S") || tabtmp01.getOp3().equals("N"))) {
					tabtmp01.enviarMensagem(null, "INFORME �S� OU �N� ");
					sessao.setAtributo("TABTM001", this);
					result.include("tabtmp01", tabtmp01);
					sessao.setAtributo("TABTM001", this);
					tabtmp01.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
					return;
				}
				if (tabtmp01.getOp3().equals("N") || UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
					faseproc = "0";
					result.include("tabtmp01", tabtmp01);
					tabtmp01.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
					return;
				}
				vwDvetab.exclui();
				// dao.commit();
				faseproc = "0";
				tabtmp01.setMensagemSaida("REGISTRO DELETADO OK    (TECLE ENTER)  ");
				sessao.setAtributo("TABTM001", this);
				result.include("tabtmp01", tabtmp01);
				sessao.setAtributo("TABTM001", this);
				tabtmp01.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				return;
			}
		}
		case "3": {

			sistha01.receberTela(false, result);
			funcoes.tabsatu(sessao);
			if (!funcoes.camret.equals("")) {
				tabtmp01.enviarMensagem(funcoes.camret, "");
				// sessao.setAtributo ("TABTM001", this);
				result.include("tabtmp01", tabtmp01);
				tabtmp01.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (!sistha01.getOp2().trim().equals("")) {

				vwDvetab.setDataAtualizacao(data);
				vwDvetab.setMatriculaFunc(sessao.getSisgsisp().getMatric());
				vwDvetab.setCodigoTabela(889);
				vwDvetab.setCodigoElem(0L);
				vwDvetab.setDescricaoElem(descel1);
				vwDvetab.setChaveAlfaAcesso(tabtmp01.getChalfa1());
				vwDvetab.setDataApoio(datapo);
				vwDvetab.atualiza();
				// dao.commit();
				// dao.rollback();
				tabtmp01.setMensagemSaida(" REGISTRO ALTERADO OK.");
				faseproc = "0";
			}
			result.include("tabtmp01", tabtmp01);
			tabtmp01.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		case "4": {
			tabtmp01.receberTela(false, result);
			if (!tabtmp01.getOperacao().equals("ATE")) {
				saida();
			}
			if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
				tabtmp01.setMensagemSaida("REGISTRO NAO INCLUIDO");
				break;
			}
			if (!UtilBean.funcao.equals(UtilBean.ctt_confirma)) {
				tabtmp01.enviarMensagem(null, "TECLE PF5 PARA ATUALIZAR");
				return;
			} else {

				sistha01.setOp3("X");
			}

			faseproc = "5";
			sessao.setAtributo("TABTM001", this);
			result.include("sistha02", sistha02);
			sistha02.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		case "5": {
			sistha02.receberTela(false, result);
			if (!tabtmp01.getOp3().trim().equals("")) {

				vwDvetab.setDataAtualizacao(data);
				vwDvetab.setMatriculaFunc(sessao.getSisgsisp().getMatric());
				vwDvetab.setCodigoTabela(889);
				vwDvetab.setChaveAlfaAcesso(tabtmp01.getChalfa1());
				vwDvetab.setDescricaoElem(descel1);
				vwDvetab.setDataApoio(datapo);
				vwDvetab.grava();
				// dao.commit();
				// dao.rollback();
				tabtmp01.setMensagemSaida(" REGISTRO INCLUIDO OK.");
				faseproc = "0";
			}
			sessao.setAtributo("TABTM001", this);
			result.include("tabtmp01", tabtmp01);
			tabtmp01.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}

		}
		sessao.setAtributo("TABTM001", this);
		result.include("tabtmp01", tabtmp01);
		if (!result.used()) {
			tabtmp01.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
		}
	}

	private void saida() {
		funcoes.sissm001(result, sessao);
	}

	private void restaurarEstados(Sistha01 psistha01, Sistha02 psistha02, Tabtmp01 ptabtmp01) {
		this.tabtm001Anterior = (Tabtm001) sessao.getAtributo("TABTM001");
		if (psistha01 != null) {
			sistha01 = psistha01;
		} else {
			sistha01 = this.tabtm001Anterior.sistha01;
		}
		sistha01.setSessao(sessao);
		if (psistha02 != null) {
			sistha02 = psistha02;
		} else {
			sistha02 = this.tabtm001Anterior.sistha02;
		}
		sistha02.setSessao(sessao);
		if (ptabtmp01 != null) {
			tabtmp01 = ptabtmp01;
		} else {
			tabtmp01 = this.tabtm001Anterior.tabtmp01;
		}
		tabtmp01.setSessao(sessao);
		this.faseproc = this.tabtm001Anterior.faseproc;
	}
}
